package com.atguigu.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;



@Component
public class ConfirmReceiver {

    /**
     *
     * @param msg  发送的消息
     * @param channel  监听管道
     * @param message
     */
    //监听发送的消息 product--->exchange--routingKey-->queue--->consumer
    @SneakyThrows
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(value = "queue.confirm",durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = "exchange.confirm"),
            key = {"routing.confirm"}
    ))
    public void getMsg(String msg, Channel channel, Message message){
        System.out.println("接收到的消息：\t"+msg);
        System.out.println("接收到的消息：\t"+new String(message.getBody()));

        //  手动确认！   第一个参数：表示消息唯一表示，第二个参数表示是否批量确认！
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}